<template>
  <el-form ref="accountRef" :model="account" :rules="rules" label-width="60px">
    <el-form-item label="帐号" prop="name">
      <el-input v-model="account.name"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="name">
      <el-input v-model="account.password"></el-input>
    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { ElForm } from 'element-plus'
import cache from '@/utils/cache'
import { rules } from '../config/account-rule'
export default defineComponent({
  setup() {
    const account = reactive({
      name: cache.getCache('name') ?? 'codertest',
      password: cache.getCache('password') ?? '123456'
    })
    const store = useStore()
    const accountRef = ref<InstanceType<typeof ElForm>>()
    const loginAction = (isKeepPassword: boolean) => {
      accountRef.value?.validate((valid) => {
        if (valid) {
          // 判断是否需要记住密码
          if (isKeepPassword) {
            cache.setCache('name', account.name)
            cache.setCache('password', account.password)
          } else {
            cache.removeCache('name')
            cache.removeCache('password')
          }
          store.dispatch('login/accountLoginAction', { ...account })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
    return {
      account,
      rules,
      loginAction,
      accountRef
    }
  }
})
</script>

<style scoped></style>
